package com.liuzy.openapi;

import org.json.JSONException;

import com.liuzy.tool.HTTP;
import com.liuzy.tool.JSON;

/**
 * 百度API
 * @author liuzy
 * @since 2015年10月18日
 */
public class BaiduApi {
	public static void main(String[] args) {
		System.out.println(translate("hello word!"));
		System.out.println(queryIP("122.225.69.0"));
	}
	public static String id = "7071885";
	public static String apiKey = "od4SDEdGxa3eGmfFmPdXqEUp";
	private static String secretKey = "d3OdG67vqtuknaziXmGjl9eBsIFbKFjQ";
	public static String access_token = "24.c327fc05ccb5833e26525a75aa2bb4be.2592000.1447687407.282335-7071885";
	public static String session_key;
	public static String scope;
	public static String refresh_token;
	public static String session_secret;
	public static String shortUrl(String url) {
		String jsonString = new HTTP().put2body("url", url).doPOST("http://dwz.cn/create.php");
		try {
			JSON json = new JSON(jsonString);
			return json.get("tinyurl").value();
		} catch (Exception e) {
			return jsonString;
		}
	}
	/** 百度翻译 */
	public static String translate(String text) {
		HTTP client = new HTTP();
		String url = "http://openapi.baidu.com/public/2.0/bmt/translate";
		client.put2url("client_id", apiKey).put2url("from", "auto").put2url("to", "auto").put2url("q", text);
		String jsonString = client.doGET(url);
		try {
			JSON json = new JSON(jsonString);
			return json.getArray("trans_result", 0).get("dst").value();
		} catch (JSONException e) {
			e.printStackTrace();
			return jsonString;
		}
	}
	/** IP归属 */
	public static String queryIP(String ip) {
		HTTP client = new HTTP();
		String url = "https://openapi.baidu.com/rest/2.0/iplib/query";
		client.put2url("access_token", access_token).put2url("ip", ip);
		String jsonString = client.doGET(url);
		try {
			JSON json = new JSON(jsonString);
			return json.get(ip).get("province").value() + json.get(ip).get("city").value();
		} catch (JSONException e) {
			e.printStackTrace();
			return jsonString;
		}
	}
	public static String getAccessToken() {
		HTTP client = new HTTP();
		String url = "https://openapi.baidu.com/oauth/2.0/token";
		client.put2body("grant_type", "client_credentials")
			.put2body("client_id", apiKey)
			.put2body("client_secret", secretKey);
		String jsonString = client.doPOST(url);
		try {
			JSON json = new JSON(jsonString);
			access_token = json.get("access_token").value();
			session_key = json.get("session_key").value();
			scope = json.get("scope").value();
			refresh_token = json.get("refresh_token").value();
			session_secret = json.get("session_secret").value();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return access_token;
	}
	public static String getLoggedInUser() {
		HTTP client = new HTTP();
		String url = "https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser";
		client.put2url("access_token", access_token);
		String jsonString = client.doGET(url);
		return jsonString;
	}
	public static String getInfo() {
		HTTP client = new HTTP();
		String url = "https://openapi.baidu.com/rest/2.0/passport/users/getInfo";
		client.put2url("access_token", access_token);
		String jsonString = client.doGET(url);
		return jsonString;
	}
	public static String getFriends() {
		HTTP client = new HTTP();
		String url = "https://openapi.baidu.com/rest/2.0/friends/getFriends";
		client.put2url("access_token", access_token);
		String jsonString = client.doGET(url);
		return jsonString;
	}
}
